using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.Xml.Linq;
namespace MicroMsg.Scene.Video
{
	public class DownloadVideoService
	{
		private const string TAG = "DownloadVideoService";
		private const int MAX_RUNNING = 5;
		private static TimerObject mTimerObject;
		public bool doSence(int msgSvrId)
		{
			DownloadVideoContext downloadVideoContext = DownloadVideoContextMgr.getInstance().findBySrvMsgID(msgSvrId);
			if (downloadVideoContext != null && !downloadVideoContext.needToClean())
			{
				Log.smethod_1("DownloadVideoService", "already downloading video  by msgid =  " + msgSvrId);
				return false;
			}
			MsgTrans byMsgSvrID = StorageMgr.msgImg.getByMsgSvrID(msgSvrId);
			if (byMsgSvrID == null)
			{
				Log.smethod_1("DownloadVideoService", "Not found the video information , failed to downLoad video, msgid = " + msgSvrId);
				return false;
			}
			byMsgSvrID.nTransDataLen = (int)StorageIO.fileLength(byMsgSvrID.strImagePath);
			ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(ChatMsgHelper.getTalker(byMsgSvrID.strToUserName, byMsgSvrID.strFromUserName), msgSvrId);
			if (bySvrID == null)
			{
				Log.smethod_1("DownloadVideoService", "Not found the chatmsg by msgid =  " + msgSvrId);
				return false;
			}
			Log.smethod_5("DownloadVideoService", "begin downLoad  video...msgid = " + msgSvrId);
			DownloadVideoContext downloadVideoContext2 = new DownloadVideoContext();
			downloadVideoContext2.mVideoInfo = byMsgSvrID;
			downloadVideoContext2.mChatMsg = bySvrID;
			DownloadVideoService.parseVideoMsgXML(bySvrID.strMsg, byMsgSvrID);
			downloadVideoContext2.updateProgressInfo(0);
			downloadVideoContext2.updateChatMsg();
			DownloadVideoContextMgr.getInstance().putToHead(downloadVideoContext2);
			this.checkReadyContextDispatcher();
			return true;
		}
		public bool doCancelScene(int msgSvrId)
		{
			DownloadVideoContext downloadVideoContext = DownloadVideoContextMgr.getInstance().findBySrvMsgID(msgSvrId);
			if (downloadVideoContext == null)
			{
				return false;
			}
			if (downloadVideoContext.mStatus != 5)
			{
				if (downloadVideoContext.mStatus != 4)
				{
					if (downloadVideoContext.mScene == null)
					{
						return false;
					}
					downloadVideoContext.mScene.doCancel();
					return true;
				}
			}
			return false;
		}
		public DownloadVideoContext.ProgressInfo getProgress(int msgSvrId)
		{
			MsgTrans byMsgSvrID = StorageMgr.msgImg.getByMsgSvrID(msgSvrId);
			if (byMsgSvrID != null)
			{
				DownloadVideoContext.ProgressInfo progressInfo = new DownloadVideoContext.ProgressInfo();
				progressInfo.totalLength = byMsgSvrID.nTotalDataLen;
				progressInfo.gotLength = byMsgSvrID.nTransDataLen;
				progressInfo.msgSvrId = byMsgSvrID.nMsgSvrID;
				progressInfo.status = DownloadVideoContext.convertStatus(byMsgSvrID.nStatus);
				progressInfo.printInfo();
				return progressInfo;
			}
			return null;
		}
		private void checkReadyContextDispatcher()
		{
			if (DownloadVideoService.mTimerObject == null)
			{
				Log.smethod_5("DownloadVideoService", "Download video service dispatcher startup. ");
				DownloadVideoService.mTimerObject = TimerService.addTimer(2, new EventHandler(this.onVideoDownContextDispatcher), 1, -1);
				DownloadVideoService.mTimerObject.start();
			}
		}
		public void onVideoDownContextDispatcher(object sender, EventArgs e)
		{
			DownloadVideoContextMgr.getInstance().clearnFinishedContext();
			if (DownloadVideoContextMgr.getInstance().getCount() <= 0)
			{
				Log.smethod_5("DownloadVideoService", "all ready, close dispatcher timer. ");
				DownloadVideoService.mTimerObject.stop();
				DownloadVideoService.mTimerObject = null;
				return;
			}
			if (DownloadVideoContextMgr.getInstance().countRunningContext() >= 5)
			{
				return;
			}
			DownloadVideoContext firstContextNeedHandle = DownloadVideoContextMgr.getInstance().getFirstContextNeedHandle();
			if (firstContextNeedHandle == null)
			{
				return;
			}
			Log.smethod_4("DownloadVideoService", "new task startup, msgid = " + firstContextNeedHandle.mVideoInfo.nMsgSvrID);
			NetSceneDownloadVideo netSceneDownloadVideo = new NetSceneDownloadVideo();
			netSceneDownloadVideo.doScene(firstContextNeedHandle);
		}
		public static bool parseVideoMsgXML(string xmlStr, MsgTrans videoinfo)
		{
			if (!string.IsNullOrEmpty(xmlStr) && videoinfo != null)
			{
				bool result;
				try
				{
					XDocument xDocument = XDocument.Parse(xmlStr);
					XElement xElement = xDocument.Element("msg").Element("videomsg");
					XAttribute xAttribute = xElement.Attribute("clientmsgid");
					if (xAttribute != null)
					{
						videoinfo.strClientMsgId = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("length");
					if (xAttribute != null)
					{
						videoinfo.nTotalDataLen = int.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("playlength");
					if (xAttribute != null)
					{
						videoinfo.nDuration = int.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("fromusername");
					if (xAttribute != null)
					{
						videoinfo.strFromUserName = xAttribute.get_Value();
					}
					return true;
				}
				catch (Exception ex)
				{
					Log.smethod_5("DownloadVideoService", ex.get_Message());
					result = false;
				}
				return result;
			}
			Log.smethod_5("DownloadVideoService", "failed to parse msg xml, input invalid para");
			return false;
		}
	}
}
